package com.polaris.live.im.sfs.processor.impl

import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.polaris.live.common.util.UserAccountUtils
import com.polaris.live.im.sfs.COMMAND_ACCOUNT_CHANGE
import com.polaris.live.im.sfs.bean.pusher.PusherNoticeMessage
import com.polaris.live.im.sfs.core.SmartFoxManager
import com.polaris.live.im.sfs.core.bean.DataArgument
import com.polaris.live.im.sfs.processor.AbstractMessageProcessor
import kotlinx.coroutines.withContext

/**
 * 用户账号变更
 *
 * @author Created by lucas on 2023/10/23 15:32
 * @since 1.0.0
 */
class AccountChangeMessageProcessor : AbstractMessageProcessor() {

    override fun shouldProcess(type: Int, command: String): Boolean {
        return command == COMMAND_ACCOUNT_CHANGE
    }

    override suspend fun handle(type: Int, command: String, argument: DataArgument) {
        val data = argument.getData<PusherNoticeMessage>() ?: return

        val element = JsonParser.parseString(data.content)
        if (element is JsonObject) {
            withContext(SmartFoxManager.noticeDispatcher) {
                UserAccountUtils.handleAccount(element)
            }
        }
    }
}